package errcode

import "fmt"

type ErrCode int

const (
	Successes       ErrCode = 0   //ok
	ErrBadRequest   ErrCode = 400 //请求错误
	ErrUnauthorized ErrCode = 401 //请求未授权
	ErrForbidden    ErrCode = 403 //资源被禁用
	ErrNotFound     ErrCode = 404 //资源未找到或没有权限访问
	ErrServerError  ErrCode = 500 //服务器内部错误

	ErrUserOrPwd        ErrCode = 4000 //用户名或者密码错误
	ErrBadCaptcha       ErrCode = 4001 //验证码错误
	ErrBadRequestParams ErrCode = 4002 //参数校验错误
)

type ErrWrap struct {
	errCode ErrCode
	err     error
}

func (i ErrCode) Error() string {
	return i.String()
}

func (i ErrCode) New(format string, args ...any) ErrWrap {
	return ErrWrap{
		errCode: i,
		err:     fmt.Errorf(format, args...),
	}
}

func (i ErrCode) Wrap(err error) ErrWrap {
	return ErrWrap{errCode: i, err: err}
}

func (ew ErrWrap) Error() string {
	return ew.err.Error()
}

func (ew ErrWrap) ErrCode() int {
	return int(ew.errCode)
}
